package A2;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class A2 {

    public static void main(String[] args) {
        System.out.println("服务端开始运行");
        Thread t = new Thread(()->{
            try (
                    ServerSocket serverSocket = new ServerSocket(8080);
                    Socket socket=serverSocket.accept();
                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                    DataInputStream in = new DataInputStream(socket.getInputStream());
                    BufferedReader key = new BufferedReader(new InputStreamReader(System.in))
                    ){
                while (true){
                    String str = in.readUTF();
                    JSONObject jsonObject = new JSONObject(str);
                    System.out.println("客户端接收数据"+jsonObject);
                    String key1 = key.readLine();
                    if (key1.equalsIgnoreCase("Y")){
                        break;
                    }
                    jsonObject = new JSONObject();
                    jsonObject.put("name",key1);
                    out.writeUTF(jsonObject.toString());
                    out.flush();
                }
            }catch (Throwable t1){
                t1.printStackTrace();
            }
        });
        t.start();
    }
}
